<?php
/**
 * BaseController.class.php
 * 基类控制器
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-07-28
 */

namespace Attach\Controller;

class BaseController extends \Common\Controller\BaseController {

    public $userId, $userInfo;
    public $prefixOauthCache    = 'attach_wx_oauth_info_';

    //构造函数
    function __construct(){
        parent::__construct();

        //加载平台配置信息
        if(!S('plat_config')){
            $platConfig  = M('ad_config', null)->where('id=1')->find();
            S('plat_config', $platConfig, 600);
        }
        C('PLAT_CONFIG', S('plat_config'));

        // $this->_checkRequest();
        $this->_checkLogin();

    }

    /**
     * 检测是否登录
     * @return  boolean
     */
    private function _checkLogin(){
        //用户信息获取
        $this->userInfo     = $this->_getUserInfo();
        $this->userId   = $this->userInfo['id'];

        //必须登录检测
        $controlName    = strtolower(CONTROLLER_NAME);
        $actionName     = strtolower(ACTION_NAME);

        $mustLogin  = !in_array($controlName, ['public', 'wechat']);
        if($mustLogin && !$this->userId){
            $this->response('请登录', -502);
        }
    }

    /**
     * 获取用户信息
     * @return  $userInfo
     */
    private function _getUserInfo(){
        $userId     = session('user_id');
        if($userId){
            $userInfo   = D('User')->getItem(['id' => $userId, 'status' => 1]);
            //用户信息检测
            if(!in_array($userInfo['role_id'], [101, 102, 103, 104])){
                $this->response($userInfo, -505);
                $this->response('非推广人员不可登陆!', -505);
            }

            return  $userInfo;
        }else{
            return false;
        }
    }
}